package com.koron.device.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.koron.bean.base.BaseBean;
import com.koron.bean.system.org.OrgBean;
import com.koron.common.core.util.CodeTools;
import com.koron.device.bean.census.EqMsgBo;
import com.koron.device.bean.census.EqMsgReport;
import com.koron.device.bean.census.EqPictureReport;
import com.koron.device.mapper.EquipmentMapper;
import com.koron.device.mapper.census.EqMsgReportMapper;
import com.koron.device.mapper.census.EqPictureReportMapper;
import com.koron.device.mapper.census.UserOrgMapper;
import com.koron.device.service.CensusService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@Slf4j
public class CensusServiceImpl implements CensusService {

    @Override
    @TaskAnnotation("eqmsgreport")
    public void eqmsgreport(SessionFactory factory, String mark, String appId) {

        UserOrgMapper userorgmapper = factory.getMapper(UserOrgMapper.class);
        EquipmentMapper eqmapper = factory.getMapper(EquipmentMapper.class);
        EqMsgReportMapper eqmsgreportmapper = factory.getMapper(EqMsgReportMapper.class);

        List<OrgBean> orgBeanList = userorgmapper.queryManagementOrg(appId);
        if (CollUtil.isEmpty(orgBeanList)) {
            return;
        }
        // 职能为管理的组织单位
        List<String> deptIds = orgBeanList.stream().map(BaseBean::getId).collect(Collectors.toList());
        Map<String, String> orgNamemap = orgBeanList.stream().collect(Collectors.toMap(BaseBean::getId, OrgBean::getName));


        List<EqMsgReport> insert =new ArrayList();
        for (String deptId : deptIds) {
            // 获取管理单位设备信息完整数量
            List<EqMsgBo> eqMsgBoslist = eqmapper.querEqParamMsg(deptId);
            if (CollUtil.isEmpty(eqMsgBoslist)) {
                continue;
            }
            for (EqMsgBo eqMsgBo : eqMsgBoslist) {
                EqMsgReport eqMsgReport = new EqMsgReport();
                eqMsgReport.setDeptId(deptId);
                eqMsgReport.setMark(mark);
                eqMsgReport.setAppId(appId);
                eqMsgReport.setCreateTime(new Date());
                eqMsgReport.setDeptName(orgNamemap.get(deptId));
                eqMsgReport.setId(CodeTools.getCode32());
                eqMsgReport.setWriteType(eqMsgBo.getWriteType());
                eqMsgReport.setQty(eqMsgBo.getQty());
                insert.add(eqMsgReport);
            }
        }

        eqmsgreportmapper.deleteAll();
        Lists.partition(insert, 300).stream().forEach(p-> eqmsgreportmapper.batchInsert(p));
    }

    @Override
    @TaskAnnotation("eqpicturereport")
    public void eqpicturereport(SessionFactory factory, String mark, String appId) {

        UserOrgMapper userorgmapper = factory.getMapper(UserOrgMapper.class);
        EquipmentMapper eqmapper = factory.getMapper(EquipmentMapper.class);
        EqPictureReportMapper eqpicturereportmapper = factory.getMapper(EqPictureReportMapper.class);

        List<OrgBean> orgBeanList = userorgmapper.queryManagementOrg(appId);
        if (CollUtil.isEmpty(orgBeanList)) {
            return;
        }
        // 职能为管理的组织单位
        List<String> deptIds = orgBeanList.stream().map(BaseBean::getId).collect(Collectors.toList());
        Map<String, String> orgNamemap = orgBeanList.stream().collect(Collectors.toMap(BaseBean::getId, OrgBean::getName));


        List<EqPictureReport> insert =new ArrayList();
        for (String deptId : deptIds) {
            // 获取管理单位设备信息完整数量
            List<EqMsgBo> eqMsgBoslist = eqmapper.querEqPictureMsg(deptId);
            if (CollUtil.isEmpty(eqMsgBoslist)) {
                continue;
            }
            for (EqMsgBo eqMsgBo : eqMsgBoslist) {
                EqPictureReport eqMsgReport = new EqPictureReport();
                eqMsgReport.setDeptId(deptId);
                eqMsgReport.setMark(mark);
                eqMsgReport.setAppId(appId);
                eqMsgReport.setCreateTime(new Date());
                eqMsgReport.setDeptName(orgNamemap.get(deptId));
                eqMsgReport.setId(CodeTools.getCode32());
                eqMsgReport.setUploadType(eqMsgBo.getWriteType());
                eqMsgReport.setQty(eqMsgBo.getQty());
                insert.add(eqMsgReport);
            }
        }

        eqpicturereportmapper.deleteAll();
        Lists.partition(insert, 300).stream().forEach(p-> eqpicturereportmapper.batchInsert(p));
    }
}
